<?php
/**
 * 众筹状态改变检测.
 */
require_once(dirname(__FILE__)."/../config/include.php");


$GLOBALS['DEBUG_SQL_LOG'] = false ;

sql_connect() ;

while (true) {
	// 开始众筹,时间到
    sql_query("UPDATE `crowd_item` SET `state`='1' WHERE (`start_time` = 0 OR `start_time` <= unix_timestamp()) AND `state`='0' and `useing` = 1");

	// 时间到结束众筹(成功)
    sql_query("UPDATE `crowd_item` SET `state`='2' WHERE (`end_time` != 0 AND `end_time` <= unix_timestamp()) AND `state`='1' and `useing` = 1 AND `amRaised` >= `tarAmount`");

	// 时间到结束众筹(失败)
    sql_query("UPDATE `crowd_item` SET `state`='3' WHERE (`end_time` != 0 AND `end_time` <= unix_timestamp()) AND `state`='1' and `useing` = 1 AND `amRaised` < `tarAmount`");


	// 没有可以超限的已经成功的可以提前结束

    $c_list = sql_fetch_rows("SELECT * FROM `crowd_item` WHERE (`end_time` != 0 AND `end_time` > unix_timestamp()) AND `state`='1' and `useing` = 1 AND `amRaised` >= `tarAmount`");
    foreach ($c_list as $c) {
        $crowd_id = $c['id'];
        $one_ex = sql_fetch_one("SELECT * FROM `crowd_price` WHERE  `crowd_id` = $crowd_id AND  `exceed_limit` = 1");
        if (array_key_exists("crowd_id", $one_ex)) {
            // 可以超限,不管
        } else {
            // 提前结束
            sql_query("UPDATE `crowd_item` SET `state`='2' WHERE `id` = '$crowd_id'");
        }
    }

    // 等待2秒执行一次
    usleep(1000000 * 2);
}
?>
